package cn.opopopo.wordexport.util;


import cn.opopopo.wordexport.mode.DownLoadUrlStream;
import cn.opopopo.wordexport.mode.DownLoadWord;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class DownloadUtils {
	/**
     * 
     * @param downLoadUrlStreams 文件信息 地址 流
     * @param zipName 压缩包名称
     * @param response
     * @throws IOException
     */
    public static void downloadFolder(List<DownLoadUrlStream> downLoadUrlStreams, String zipName, HttpServletResponse response) throws IOException {

        response.setContentType("application/octet-stream");

        response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(zipName,"UTF-8")  + "\"");
        Set<String> paths = new HashSet<>(); // 用于记录已经添加到压缩文件中的目录

        try {
            ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
            for (DownLoadUrlStream file : downLoadUrlStreams) {
                String virtualPath = file.getUrl().substring(1);
                String[] pathArray = virtualPath.split("/");
                //构建文件的目录结构
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < pathArray.length - 1; i++) {
                    sb.append(pathArray[i]).append("/");
                    String folderPath = sb.toString();
                    if (!paths.contains(folderPath)) { // 如果该目录还未被添加到压缩文件中，则添加目录
                        ZipEntry folderEntry = new ZipEntry(folderPath);
                        zipOut.putNextEntry(folderEntry);
                        zipOut.closeEntry();
                        paths.add(folderPath); // 将新添加的目录记录到集合中
                    }
                }
                ZipEntry entry = new ZipEntry(virtualPath);
                zipOut.putNextEntry(entry);
                //将文件流写入文件中
                InputStream inputStream =  file.getStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = inputStream.read(buffer)) > 0) {
                    zipOut.write(buffer, 0, len);
                }
                inputStream.close();
                zipOut.closeEntry();
            }
            zipOut.flush();
            zipOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void downloadFolderx(List<DownLoadWord> downLoadUrlStreams, String zipName, HttpServletResponse response) throws IOException {

        response.setContentType("application/octet-stream");

        response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(zipName,"UTF-8")  + "\"");
        Set<String> paths = new HashSet<>(); // 用于记录已经添加到压缩文件中的目录

        try {
            ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
            for (DownLoadWord file : downLoadUrlStreams) {
                String virtualPath = file.getUrl().substring(1);
                String[] pathArray = virtualPath.split("/");
                //构建文件的目录结构
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < pathArray.length - 1; i++) {
                    sb.append(pathArray[i]).append("/");
                    String folderPath = sb.toString();
                    if (!paths.contains(folderPath)) { // 如果该目录还未被添加到压缩文件中，则添加目录
                        ZipEntry folderEntry = new ZipEntry(folderPath);
                        zipOut.putNextEntry(folderEntry);
                        zipOut.closeEntry();
                        paths.add(folderPath); // 将新添加的目录记录到集合中
                    }
                }
                ZipEntry entry = new ZipEntry(virtualPath);
                zipOut.putNextEntry(entry);
                //将文件流写入文件中

                XWPFDocument xwpfDocument = file.getXwpfDocument();

                xwpfDocument.write(zipOut);

            }
            zipOut.flush();
            zipOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  }
   
